import React, { ReactNode } from 'react';
import { Navigate } from 'react-router-dom';
import { usePermission, AccessRole } from '../contexts/PermissionContext';

interface PermissionRouteProps {
    /**
     * 需要的权限角色，可以是单个角色或角色数组
     */
    requiredRole?: AccessRole | AccessRole[];

    /**
     * 是否需要编辑基础信息的权限
     */
    requireEditBasicInfo?: boolean;

    /**
     * 是否需要访问权限管理页面的权限
     */
    requirePermissionAccess?: boolean;

    /**
     * 是否需要编辑功能的权限
     */
    requireEditFeatures?: boolean;

    /**
     * 是否需要访问导入页面的权限
     */
    requireImportAccess?: boolean;

    /**
     * 是否需要访问导出页面的权限
     */
    requireExportAccess?: boolean;

    /**
     * 重定向路径
     */
    redirectTo?: string;

    /**
     * 子组件
     */
    children: ReactNode;
}

/**
 * 权限路由守卫组件
 * 用于根据用户权限控制路由的访问
 */
const PermissionRoute: React.FC<PermissionRouteProps> = ({
    requiredRole,
    requireEditBasicInfo = false,
    requirePermissionAccess = false,
    requireEditFeatures = false,
    requireImportAccess = false,
    requireExportAccess = false,
    redirectTo = '/student',
    children
}) => {
    const {
        hasPermission,
        canEditBasicInfo,
        canAccessPermissionPage,
        canUseEditFeatures,
        canAccessImportPage,
        canAccessExportPage
    } = usePermission();

    // 检查是否有权限
    let hasAccess = true;
    const accessChecks = [];

    // 检查特定角色权限
    if (requiredRole) {
        const roleCheck = hasPermission(requiredRole);
        accessChecks.push({ type: '角色权限', required: requiredRole, result: roleCheck });
        if (!roleCheck) hasAccess = false;
    }

    // 检查编辑基础信息权限
    if (requireEditBasicInfo) {
        const basicInfoCheck = canEditBasicInfo();
        accessChecks.push({ type: '编辑基础信息权限', result: basicInfoCheck });
        if (!basicInfoCheck) hasAccess = false;
    }

    // 检查访问权限管理页面权限
    if (requirePermissionAccess) {
        const permissionCheck = canAccessPermissionPage();
        accessChecks.push({ type: '权限管理页面访问权限', result: permissionCheck });
        if (!permissionCheck) hasAccess = false;
    }

    // 检查编辑功能权限
    if (requireEditFeatures) {
        const editCheck = canUseEditFeatures();
        accessChecks.push({ type: '编辑功能权限', result: editCheck });
        if (!editCheck) hasAccess = false;
    }

    // 检查访问导入页面权限
    if (requireImportAccess) {
        const importCheck = canAccessImportPage();
        accessChecks.push({ type: '导入页面访问权限', result: importCheck });
        if (!importCheck) hasAccess = false;
    }

    // 检查访问导出页面权限
    if (requireExportAccess) {
        const exportCheck = canAccessExportPage();
        accessChecks.push({ type: '导出页面访问权限', result: exportCheck });
        if (!exportCheck) hasAccess = false;
    }

    // 输出权限检查结果
    console.log('权限检查结果:', accessChecks);
    console.log('最终访问权限:', hasAccess ? '允许访问' : '拒绝访问，重定向到' + redirectTo);

    // 如果有权限，显示子组件，否则重定向
    return <>{hasAccess ? children : <Navigate to={redirectTo} replace />}</>;
};

export default PermissionRoute;
